package cn.tedu.boot21.controller;

import cn.tedu.boot21.entity.User;
import cn.tedu.boot21.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {

    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/reg")
    public String reg(User user){
        User u=mapper.selectByUsername(user.getUsername());
        if (u!=null){
            return "该用户已存在!";
        }
        mapper.insert(user);
        return "注册成功";
    }

    @RequestMapping("/login")
    public String login(User user){
        User u=mapper.selectByUsername(user.getUsername());
        if (u!=null) {
            if (user.getPassword().equals(u.getPassword())) {
                return "登录成功!<a href='/'>返回首页</a>";
            }
            return "密码错误!<a href='/login.html'>重新登录</a>";
        }
        return "用户名不存在!<a href='/login.html'>重新登录</a>";
    }

    @RequestMapping("/regA")
    public int regA(@RequestBody User user){
        User u=mapper.selectByUsername(user.getUsername());
        if (u!=null){
            return 2;//用户名已存在
        }
        mapper.insert(user);
        return 1;//注册成功
    }

    @RequestMapping("/loginA")
    public int loginA(@RequestBody User user){
        User u=mapper.selectByUsername(user.getUsername());
        if (u!=null){
            if (user.getPassword().equals(u.getPassword())){
                return 1;//登录成功!
            }
            return 3;//密码错误!
        }
        return 2;//用户名不存在
    }


}
